#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

#====================================#
# 系统要求: CentOS 6,7,8				 #
# 描述: 一键安装Valheim服务器				 #
# 作者: Was Yesterday by 永夜					 #
# 联系方式: QQ群：618852815    #
#====================================#

clear
echo -e "\033[34m================================================================\033[0m

            欢迎使用 Valheim服务器 一键安装脚本

            系统要求:  CentOS 6,7,8
            描述: 一键安装 Valheim 服务器
            作者: Was Yesterday  改造git raw支持  by 永夜
            联系方式： QQ群：618852815

\033[34m================================================================\033[0m"

echo

# 当前文件夹
Current_Dir=`pwd`

# 检查用户是否为root
function checkRoot() {
	if [[ $EUID -ne 0 ]]; then
		echo -e "\033[31m 错误: 本脚本必须以root用户执行! \033[0m"
		exit 1
	else
		echo -e "Root用户检查完成"
	fi
}

# 检查系统版本
function checkOS() {
	if [[ -f /etc/redhat-release ]]; then
		OS=`cat /etc/redhat-release`
		echo -e "系统版本检查完成,当前系统为: ${OS}"
	else
		echo -e "\033[31m 目前脚本不支持当前操作系统! \033[0m"
		exit 1
	fi
}

# 安装steamcmd依赖
function installPackage() {
	read -r -p "请回车继续安装, 或者按 Ctrl+C 停止安装"
	yum update -y

	# cmd = "cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'"
	# echo -e "当前系统大版本为: ${osv}"
	# if [$osv -gt 7];
	# then
		# echo -e "为centos版本大于8的安装epel和tar"
		yum install -y epel-release tar
	# fi

	echo -e "开始安装依赖: wget screen glibc.i686 libstdc++.i686 ......"
	yum install -y wget screen glibc.i686 libstdc++.i686
	echo -e "依赖安装完成"
}

# 创建新用户,下载steamcmd并解压,删除安装包
function createUser() {
	read -p "请输入管理steamcmd的用户名(直接回车的话,默认为steam): " steamUser
	[ -z "${steamUser}" ] && steamUser='steam'
	read -p "请输入steamcmd文件夹名称(直接回车的话,默认为steamcmd): " steamcmdFolder
	[ -z "${steamcmdFolder}" ] && steamcmdFolder='steamcmd'
	
	# 创建并切换到该用户根目录下
	useradd -m ${steamUser}
	# su ${steamUser}
	cd "/home/${steamUser}"

	# 创建文件夹
	mkdir ${steamcmdFolder}
	cd ${steamcmdFolder}
	
	# 下载steamcmd并解压
	if ! wget --no-check-certificate https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz; then
		echo -e "\033[31m 下载 steamcmd 文件失败,请检查网络状态. \033[0m"
		exit 1
	fi
	tar zxvf steamcmd_linux.tar.gz
	rm -f steamcmd_linux.tar.gz

	echo
	echo "------------------------------"
	echo "成功设置用户${steamUser}"
	echo "成功创建文件夹${steamcmdFolder}"
	echo "成功下载并解压steamcmd"
	echo "------------------------------"
	echo
}

# 安装Valheim服务器端
function installValheim() {
	read -p "请输入Valheim服务器端文件夹名称(直接回车的话,默认为Valheim_server): " ValheimFolder
	[ -z "${ValheimFolder}" ] && ValheimFolder='Valheim_server'
	
	echo -e "正在写入安装Valheim服务器脚本......"
	touch install_Valheim.txt
	echo "login anonymous" >> install_Valheim.txt
	echo "force_install_dir ../${ValheimFolder}" >> install_Valheim.txt
	echo "app_update 896660 validate" >> install_Valheim.txt
	echo "quit" >> install_Valheim.txt

	echo "准备安装Valheim服务器端,接下来的操作取决于网络速度(5分钟左右)."
	read -r -p "请回车继续安装, 或者按 Ctrl+C 停止安装"
	./steamcmd.sh +runscript install_Valheim.txt
	echo -e "安装Valheim服务器端完成"
}

# 升级Valheim服务器端
function updateValheim() {
	echo -e "正在写入更新Valheim服务器脚本......"
	touch update_Valheim.txt
	echo "login anonymous" >> update_Valheim.txt
	echo "force_install_dir ../${ValheimFolder}" >> update_Valheim.txt
	echo "app_update 896660" >> update_Valheim.txt
	echo "quit" >> update_Valheim.txt
	./steamcmd.sh +runscript update_Valheim.txt
	echo -e "写入更新Valheim服务器脚本完成"
}

function openFirewalld(){
	echo -e "启动防火墙"
	systemctl start firewalld
	echo -e "开放2456-2458端口TCP、UDP"
	firewall-cmd --zone=public --add-port=2456/tcp --permanent
	firewall-cmd --zone=public --add-port=2457/tcp --permanent
	firewall-cmd --zone=public --add-port=2458/tcp --permanent
	firewall-cmd --zone=public --add-port=2456/udp --permanent
	firewall-cmd --zone=public --add-port=2457/udp --permanent
	firewall-cmd --zone=public --add-port=2458/udp --permanent
	echo -e "设置防火墙开机自启"
	systemctl enable firewalld
	echo -e "重启防火墙，端口生效"
	firewall-cmd --reload
}

# 修改权限
function chmodSteam() {
	cd "/home/${steamUser}"
	mv "${Current_Dir}/Steam/" "/home/${steamUser}/"
	mv "${Current_Dir}/.steam/" "/home/${steamUser}/"
	chown -R steamUser *
}

# 安装完成, 向用户展示安装信息
function showInfo() {
	cd Current_Dir
	clear

	echo -e "---------------安装完成-----------------"
	echo
	echo -e "你的steamcmd用户名: ${steamUser}"
	echo -e "你的steamcmd文件夹目录: /home/${steamUser}/${steamcmdFolder}"
	echo -e "你的Valheim服务器文件夹目录: /home/${steamUser}/${ValheimFolder}"
	echo -e "steamcmd安装Valheim服务器脚本文件(位于${steamcmdFolder}中): install_Valheim.txt"
	echo -e "steamcmd更新Valheim服务器脚本文件(位于${steamcmdFolder}中): update_Valheim.txt"
	echo
	echo -e "screen已安装"
	echo
	echo -e "--------------------------------------"
	echo
	exit 1
}


# 安装步骤
# 检查root权限
checkRoot
# 检查系统版本
checkOS
# 防火墙设置
openFirewalld
# 安装依赖
installPackage
# 创建用户
createUser
# 安装Valheim服务器端
installValheim
# 更新Valheim服务器端
updateValheim
# 修改权限
chmodSteam
# 安装完成
showInfo
